//
//  TodaySubjectViewController.m
//  口袋
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "TodaySubjectViewController.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "TodaySubjectTopView.h"
#import "TodayNewsTableViewCell.h"
#import "TodaySubjectModel.h"
#import "NewsDetailViewController.h"
#import "ShowImageViewController.h"
#import "NewsPicCell.h"
#import "DKNightVersion.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface TodaySubjectViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation TodaySubjectViewController
- (void)dealloc
{
    [_array release];
    [_bigDic release];
    [_modelArray release];
    [_skipID release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.modelArray = [NSMutableArray array];
        NSLog(@"专题");
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/nc/special/%@.html", self.skipID];
    NSLog(@"url = %@", str);
    [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
        self.bigDic = [result objectForKey:self.skipID];
        NSArray * array = [NSArray array];
        array = [self.bigDic objectForKey:@"topics"];
        
        for (NSDictionary *bigDic in array) {
            NSArray *arr = [bigDic objectForKey:@"docs"];
            NSMutableDictionary * big = [NSMutableDictionary dictionary];
            NSMutableArray * arr1 = [NSMutableArray array];
            NSString * tname = [bigDic objectForKey:@"tname"];
            [big setObject:tname forKey:@"tname"];
            for (NSDictionary *smallDic in arr) {
                TodaySubjectModel *model = [[TodaySubjectModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [arr1 addObject:model];
                [model release];
            }
            [big setObject:arr1 forKey:@"array"];
            [self.array addObject:big];
        }
        [self createTable];
            }];
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    self.table.rowHeight = HEIGHT*0.15;
    self.table.delegate = self;
    self.table.dataSource = self;
     [self.table setSeparatorColor:[UIColor lightGrayColor]];
    TodaySubjectTopView *topView = [[TodaySubjectTopView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.235 * HEIGHT)];
    topView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [topView.imageView sd_setImageWithURL:[NSURL URLWithString:[self.bigDic objectForKey:@"banner"]]];
    topView.contentLabel.text = [self.bigDic objectForKey:@"digest"];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGRect rect = [topView.contentLabel.text boundingRectWithSize:CGSizeMake(WIDTH - 0.067 * WIDTH * 2, 1000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    [topView.contentLabel setFrame:CGRectMake(0.067 * WIDTH, topView.imageView.frame.size.height + 0.018 * WIDTH, WIDTH - 0.067 * WIDTH * 2,rect.size.height)];
    topView.contentLabel.textColor = [UIColor grayColor];
    topView.contentLabel.nightBackgroundColor = UIColorFromRGB(0x343434);
    topView.contentLabel.font = [UIFont systemFontOfSize:14];
    topView.contentLabel.numberOfLines = 0;
    
    [self.view addSubview:self.table];
    [self.table setTableHeaderView:topView];
    [topView release];
    
    
    [self.table release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSArray *array = [self.array objectAtIndex:section];
    NSDictionary * dic = [self.array objectAtIndex:section];
    NSArray * arr = [dic objectForKey:@"array"];
   // NSArray *arr = [dic objectForKeyedSubscript:@"docs"];
    return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [self.array objectAtIndex:indexPath.section];
    NSArray *smallArray = [dic objectForKey:@"array"];
    TodaySubjectModel * model = [smallArray objectAtIndex:indexPath.row];
    if ([model.skipType isEqualToString:@"photoset"]) {
        NewsPicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[NewsPicCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        }
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.gif"]];
        NSDictionary *dic1 = [model.imgextra firstObject];
        NSDictionary *dic2 = [model.imgextra lastObject];
        NSString *path1 = [dic1 objectForKey:@"imgsrc"];
        NSString *path2 = [dic2 objectForKey:@"imgsrc"];
        [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:path1] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        [cell.imageView3 sd_setImageWithURL:[NSURL URLWithString:path2] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        cell.label.text = model.title;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        
        
        TodayNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[TodayNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        NSDictionary * dic = [self.array objectAtIndex:indexPath.section];
        NSArray *smallArray = [dic objectForKey:@"array"];
        TodaySubjectModel * model = [smallArray objectAtIndex:indexPath.row];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        cell.titleLabel.text = model.title;
        cell.contentLabel.text = model.digest;
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            CGFloat count = b * 1.0 / 10000 * 1.0;
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %.2f万跟帖", count] forState:UIControlStateNormal];
        } else {
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %@跟帖", model.replyCount] forState:UIControlStateNormal];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = [self.array objectAtIndex:section];
    NSString *title = [dic objectForKey:@"tname"];
    return title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [self.array objectAtIndex:indexPath.section];
    NSArray *smallArray = [dic objectForKey:@"array"];
    TodaySubjectModel * model = [smallArray objectAtIndex:indexPath.row];
    if ([model.skipType isEqualToString:@"photoset"]) {
        return HEIGHT / 5.5;
    } else {
        return HEIGHT * 0.15;
    }
}

#pragma mark -
#pragma mark 跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [self.array objectAtIndex:indexPath.section];
    NSArray *smallArray = [dic objectForKey:@"array"];
    TodaySubjectModel * model = [smallArray objectAtIndex:indexPath.row];
    
    if ([model.skipType isEqualToString:@"photoset"]) {
        ShowImageViewController *show = [[ShowImageViewController alloc]init];
        show.skipID = model.photosetID;
        [self.navigationController pushViewController:show animated:YES];
        [show release];
    } else {
        NewsDetailViewController *detail = [[NewsDetailViewController alloc]init];
        detail.docID = model.docid;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
    }
}


@end
